﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data;
using VSDBM.Code;
using System.IO;

public class PluginsDAO
{

    public static String add(Plugins obj)
    {
        try
        {
            MySqlConnection connection = Connection.getConnection();
            MySqlCommand query = new MySqlCommand("INSERT INTO plugins (description, version, localization, procedurename, status) VALUES ('" + obj.description + "', '" + obj.version + "', '" + obj.localization + "', '" + obj.procedurename + "' , 1)", connection);
            query.ExecuteNonQuery();
            return "Added!";
        }
        catch (Exception e)
        {
            return "ERRO! - " + e.Message;
        }
    }

    public static String edit(Plugins obj)
    {
        try
        {
            MySqlConnection connection = Connection.getConnection();
            MySqlCommand query = new MySqlCommand("UPDATE  plugins SET description = '" + obj.description + "', version = '" + obj.version + "', localization = '" + obj.localization + "', procedurename='" + obj.procedurename + "', status='" + obj.status + "' WHERE id = " + obj.id, connection);
            query.ExecuteNonQuery();
            return "Updated!";
        }
        catch (Exception e)
        {
            return "ERRO! - " + e.Message;
        }
    }

    public static Boolean delete(Plugins obj)
    {
        try
        {

            MySqlConnection connection = Connection.getConnection();
            MySqlCommand query = new MySqlCommand("DELETE FROM use_history_plugins WHERE pluginid =" + obj.id, connection);
            query.ExecuteNonQuery();

            MySqlCommand querysec = new MySqlCommand("DELETE FROM plugins WHERE id =" + obj.id, connection);
            querysec.ExecuteNonQuery();

            
            return true;
        }
        catch (Exception e)
        {
            return false;
        }

    }

    public static DataSet select(String filters, String orderby)
    {
        DataSet dSet = new DataSet();
        try
        {
            MySqlConnection connection = Connection.getConnection();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("SELECT * FROM plugins " + filters + " " + orderby, connection);
            adapter.Fill(dSet);
            return dSet;
        }
        catch (Exception e)
        {
            return dSet;
        }
    }

    public static List<Plugins> DSToList(DataSet ds)
    {
        List<Plugins> list = new List<Plugins>();
        try
        {
            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                DataTable registro = ds.Tables[0];
                list.Add(new Plugins(int.Parse(registro.Rows[i]["id"].ToString()), registro.Rows[i]["description"].ToString(), registro.Rows[i]["version"].ToString(), registro.Rows[i]["localization"].ToString(), registro.Rows[i]["procedurename"].ToString(), int.Parse(registro.Rows[i]["status"].ToString())));
            }

        }
        catch (Exception e) { }
        return list;
    }

    public static String ListToItensmenu(List<Plugins> list)
    {
        try
        {
            String menu = "";
            for (int i = 0; i <= list.Count - 1; i++)
            {
                menu = menu + "<li class='has-sub '> ";
                menu = menu + "<a href='../../../../VSDBM/Plugins/" + list[i].localization + "/Default.aspx'><span>" + list[i].procedurename + "</span></a> ";
                menu = menu + "</li> "; 
            }

            return menu;
        }
        catch (Exception e) { }
        return "";
    }
}